package com.feihao.zhou.demo02_Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class TestForEach {
    private static Map<String, String> map = new HashMap<>();

    public static void main(String[] args) {

        map.put("001", "小环");
        map.put("002", "小欢");
        map.put("008", "小路");
        map.put("0044", "小零零");

        //调用foreach方法：
        new TestForEach().forEach1(new BigConsumerCopy<String, String>() {
            @Override
            public void accept(String s, String s2) {
                System.out.println(s + "=" + s2);
            }
        });

    }

    //编写foreach方法：
    public static void forEach1(BigConsumerCopy<String, String> bigConsumerCopy) {
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {

            //获取键与值：
            String key = entry.getKey();
            String value = entry.getValue();
            //将获取的键与值传递给接口方法：
            bigConsumerCopy.accept(key, value);
        }
    }
}


//编写BigConsumer接口
interface BigConsumerCopy<T, K> {
    abstract void accept(T t, K k);
}
